﻿namespace Pp.UI.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Web;
    using System.Web.Mvc;
    using Core;
    using Core.Extentions;
    using Core.File;
    using Core.FluentNH;
    using Dto;
    using Globalization;
    using Models;
    using NHibernate;
    using Repository;


    public class BaseController : Controller
    {
        public ProjectRepository ProjectRepository { get { return _projectRepository = _projectRepository ?? new ProjectRepository(CurrentSession, Server); } }
        public ProjectCategoryRepository ProjectCategoryRepository { get { return _projectCategoryRepository = _projectCategoryRepository ?? new ProjectCategoryRepository(CurrentSession, Server); } }
        public CommentRepository CommentRepository { get { return _commentRepository = _commentRepository ?? new CommentRepository(CurrentSession, Server); } }
        public UserRepository UserRepository { get { return _userRepository = _userRepository ?? new UserRepository(CurrentSession, Server); } }
        public AngelRepository AngelRepository { get { return _angelRepository = _angelRepository ?? new AngelRepository(CurrentSession, Server); } }
        public AngelRequestRepository AngelRequestRepository { get { return _angelRequestRepository = _angelRequestRepository ?? new AngelRequestRepository(CurrentSession, Server); } }
        public ProjectFollowRepository ProjectFollowRepository { get { return _projectFollowRepository = _projectFollowRepository ?? new ProjectFollowRepository(CurrentSession, Server); } }
        public ProfileFollowRepository ProfileFollowRepository { get { return _profileFollowRepository = _profileFollowRepository ?? new ProfileFollowRepository(CurrentSession, Server); } }
        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }


        public ProjectRepository _projectRepository;
        public ProjectCategoryRepository _projectCategoryRepository;
        public CommentRepository _commentRepository;
        public UserRepository _userRepository;
        public AngelRepository _angelRepository;
        public AngelRequestRepository _angelRequestRepository;
        public ProjectFollowRepository _projectFollowRepository;
        public ProfileFollowRepository _profileFollowRepository;


        public ISession CurrentSession;
        public BaseController()
        {
            CurrentSession = ConfigurationItem.Instance.OpenSession();
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(UserRepository); }

            if (Request.IsAuthenticated)
            {
                var user = UserRepository.GetByUsername(User.Identity.Name);
                if (user == null)
                {
                    FormsService.SignOut();
                    filterContext.Result = RedirectToAction("Index", "Home");
                    return;
                }
                TempData["User_Picture"] = user.Id.ToMediumImageString(ImageType.Profile);
                TempData["User_FullName"] = user.GetFullName();
            }
            var lang = Session["Lang"] ?? "";
            Res.Culture = new CultureInfo(lang.ToString(), false);

        }
        protected enum MessageType
        {
            Success, Failed, Information
        }
        protected override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.Exception == null) return;
            filterContext.ExceptionHandled = true;
            if (filterContext.Exception is BussinessException)
            {
                var ex = (BussinessException)filterContext.Exception;
                filterContext.Result = View("Error", new Exception(ex.MessageText));

            }
            else if (filterContext.Exception is HttpRequestValidationException)
            {
                var ex = (HttpRequestValidationException)filterContext.Exception;
                filterContext.Result = View("Error", new Exception(Res.Message_html_written_error));
            }
            else
            {
                UserRepository.CreateMail("Hata Çıktı", MailType.Error, new List<MailModel>
                                                                          {
                                                                           new MailModel("ErrorMessage",filterContext.Exception.Message)   
                                                                          }, "mesut.cakir@hotmail.com.tr");
                filterContext.Result = View("Error", new Exception(Res.Message_Error + "<p style='display:none;'>{0}</p>".FormatWith(filterContext.Exception.Message)));
            }
        }
        protected void Flash(string message, MessageType type)
        {
            TempData["Flash_Message"] = message;
            TempData["Flash_Type"] = type.ToString();
        }
    }

}


